package com.yanqu.road.server.cmd.estatewar.activity;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.estatewar.EstateWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.palace.PalaceActivity;
import com.yanqu.road.server.manger.palace.PalaceActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.Map;

@Cmd(code = Protocol.S_PALACE_AIDE_JOIN, desc = "幕宾加入请求")
public class PalaceAideJoinCmd extends AbstractCommand {
    private int activityId;
    private long userId;
    private long serverId;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        EstateWarProto.PalaceAideJoinReqMsg reqMsg = EstateWarProto.PalaceAideJoinReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        userId = reqMsg.getUserId();
        serverId = reqMsg.getServerId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        PalaceActivity palaceActivity = PalaceActivityMgr.getPalaceActivity(activityId);
        if(null == palaceActivity){
            ret = GameErrorCode.E_PALACE_ACTIVITY_NO_FOUND;
            return;
        }
        if(player.getTitleId() < palaceActivity.getPALACE_LEVEL_LIMIT()){
            ret = GameErrorCode.E_TITLE_LEVEL_NO_ENOUGH;
            return;
        }
        if(0 == serverId){
            serverId = GameServer.getInstance().getServerId();
        }
        boolean isSameUnion = false;
        String myUnionUid = player.getUserInfo().getUnionUid();
        if(!StringUtils.isNullOrEmpty(myUnionUid)){//有商会才有可能
            Map<Long, CrossUnionMember> memberMap = CrossUnionMgr.getUnionMemberMap(myUnionUid);
            if(memberMap.containsKey(userId)){
                isSameUnion = true;
            }
        }
        UserBaseInfo applyUserInfo = UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId());
        //发送到跨服处理
        EstateWarProto.PalaceAideJoinCrossReqMsg.Builder crossMsg = EstateWarProto.PalaceAideJoinCrossReqMsg.newBuilder();
        crossMsg.setActivityId(activityId);
        crossMsg.setUserId(userId);
        crossMsg.setServerId(serverId);
        crossMsg.setApplyUserId(player.getUserId());
        crossMsg.setApplyUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(applyUserInfo));
        crossMsg.setIsSameUnion(isSameUnion);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_PALACE_AIDE_JOIN_APPLY, crossMsg);
        GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(0 != ret) {
            EstateWarProto.PalaceAideJoinRespMsg.Builder respMsg = EstateWarProto.PalaceAideJoinRespMsg.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_PALACE_AIDE_JOIN, respMsg);
        }
    }
}
